CLASS zcl_moon_i_api020 DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC .

  PUBLIC SECTION.
    INTERFACES if_rap_query_provider.
  PROTECTED SECTION.
  PRIVATE SECTION.
    TYPES:
        tt_tb TYPE STANDARD TABLE OF ymoont_api020.

    METHODS call_out_api EXPORTING out_tab TYPE tt_tb search_str TYPE string.

    METHODS get_data_from_request
      IMPORTING io_request TYPE REF TO if_rap_query_request
      EXPORTING rt_result  TYPE tt_tb
      RAISING   cx_rap_query_filter_no_range.
ENDCLASS.



CLASS ZCL_MOON_I_API020 IMPLEMENTATION.


  METHOD call_out_api.
    DATA:http_client TYPE REF TO ymoon_api.
    CREATE OBJECT http_client
      EXPORTING
        i_scenario     = 'YMOON_CS_API020'
        i_service      = 'YMOON_OS_API020_REST'
      EXCEPTIONS
        no_arrangement = 1.
    IF sy-subrc = 1.
      "out->write( 'no arrangement' ).
    ENDIF.
    CHECK sy-subrc <> 1.

    DATA param TYPE string.
    DATA res TYPE string.
    param = 'crkyCn=k200n178y018k289m050l070f1&cargMtNo=' && search_str.

    res = http_client->get( param ).


    DATA: lv_xstring TYPE xstring.
    DATA: lv_conv TYPE REF TO cl_abap_conv_codepage.
    lv_xstring = cl_abap_conv_codepage=>create_out( codepage = `UTF-8` )->convert( source = res ).
    "out->write( lv_xstring ).

    DATA:BEGIN OF ls_data,
           cntrno      TYPE c LENGTH 11,
           cntrstszcd  TYPE c LENGTH 4,
           cntrselgno1 TYPE c LENGTH 60,
           cntrselgno2 TYPE c LENGTH 60,
           cntrselgno3 TYPE c LENGTH 60,
           cargmtno    TYPE c LENGTH 19,
         END OF ls_data.
    DATA:lt_data LIKE TABLE OF ls_data.

    DATA:gv_cond TYPE c LENGTH 50 VALUE 'cntrQryBrkdQryVo'.
    DATA:gv_field TYPE c LENGTH 20.
    FIELD-SYMBOLS <fs> TYPE any.


    DATA: lv_reader          TYPE REF TO if_sxml_reader.
    DATA: lv_node            TYPE REF TO if_sxml_node.
    DATA: lv_open_node       TYPE REF TO if_sxml_open_element.
    DATA: lv_close_node      TYPE REF TO if_sxml_close_element.
    DATA: lv_value_node      TYPE REF TO if_sxml_value.
    DATA: lv_value           TYPE string.

    lv_reader = cl_sxml_string_reader=>create( lv_xstring ).

    lv_node = lv_reader->read_next_node( ).


    ASSERT lv_node IS NOT INITIAL.
    WHILE lv_reader->node_type <> if_sxml_node=>co_nt_final.
      CASE lv_reader->node_type.
        WHEN if_sxml_node=>co_nt_element_open.
          lv_open_node  ?= lv_node.

          gv_field = 'ls_data-' && lv_open_node->qname-name.

        WHEN if_sxml_node=>co_nt_element_close.
          lv_close_node ?= lv_node.

          IF lv_close_node->qname-name = gv_cond.
            APPEND ls_data TO lt_data.
            CLEAR ls_data.
          ELSE.
            ASSIGN (gv_field) TO <fs>.
            IF sy-subrc = 0.
              <fs> = lv_value.
            ENDIF.
          ENDIF.

        WHEN if_sxml_node=>co_nt_value.
          lv_value_node ?= lv_node.
          lv_value = lv_value_node->get_value( ).
        WHEN if_sxml_node=>co_nt_attribute.
      ENDCASE.
      lv_node = lv_reader->read_next_node( ).
    ENDWHILE.

    MOVE-CORRESPONDING lt_data TO out_tab .



  ENDMETHOD.


  METHOD get_data_from_request.
    TYPES:
      tt_r_cntysgn   TYPE RANGE OF ymoont_api020-cntrno,
      tt_r_mtryutlnm TYPE RANGE OF ymoont_api020-cntrstszcd.

    DATA lt_r_key  TYPE tt_r_cntysgn.
    DATA lt_r_text TYPE tt_r_mtryutlnm.


    DATA(lt_filter) = io_request->get_filter( )->get_as_ranges( ).
    DATA(ld_offset) = io_request->get_paging( )->get_offset( ).
    DATA(ld_pagesize) = io_request->get_paging( )->get_page_size( ).

    LOOP AT lt_filter INTO DATA(ls_filter).
      CASE ls_filter-name.
        WHEN 'Cntysgn'.
          lt_r_key = CORRESPONDING #( ls_filter-range ).
        WHEN 'Mtryutlnm'.
          lt_r_text = CORRESPONDING #( ls_filter-range ).

      ENDCASE.
    ENDLOOP.

    SELECT * FROM ymoont_api020
          WHERE cntrno IN @lt_r_key
            AND cntrstszcd IN @lt_r_text
          ORDER BY cntrno
          INTO TABLE @rt_result
          UP TO @ld_pagesize ROWS
          OFFSET @ld_offset .


  ENDMETHOD.


  METHOD if_rap_query_provider~select.
    TYPES:
      tt_r_cntysgn   TYPE RANGE OF ymoont_api020-cntrno,
      tt_r_mtryutlnm TYPE RANGE OF ymoont_api020-cntrstszcd.

    DATA lt_r_key  TYPE tt_r_cntysgn.
    DATA lt_r_text TYPE tt_r_mtryutlnm.


    DATA: lt_dbtab TYPE STANDARD TABLE OF ymoont_api020,
          ls_dbtab TYPE ymoont_api020.
    DATA: lt_out TYPE tt_tb.

    DATA: lt_database TYPE STANDARD TABLE OF ymoont_api020.

    DATA lt_output TYPE STANDARD TABLE OF ymoon_i_api020.
    DATA ls_output TYPE ymoon_i_api020.
    DATA(lo_system_uuid) = NEW cl_system_uuid( ).

    DATA(lv_search) = io_request->get_search_expression( ).
    DATA(lt_filter) = io_request->get_filter( )->get_as_ranges( ).

    IF lt_filter IS NOT INITIAL.
      LOOP AT lt_filter INTO DATA(ls_filter).
        CASE ls_filter-name.
          WHEN 'CARGMTNO'.
            LOOP AT ls_filter-range INTO DATA(ls_field).
              call_out_api( IMPORTING out_tab = lt_out search_str = ls_field-low ).
              LOOP AT lt_out INTO DATA(ls_out).
                CLEAR ls_dbtab.
                MOVE-CORRESPONDING ls_out TO ls_dbtab.
                TRY.
                    ls_dbtab-uuid = lo_system_uuid->if_system_uuid~create_uuid_x16( ).
                  CATCH cx_uuid_error.
                ENDTRY.

                ls_dbtab-regid = sy-uname.
                ls_dbtab-regdate = sy-datum.
                ls_dbtab-cargmtno = ls_field-low.
                APPEND ls_dbtab TO lt_dbtab.


              ENDLOOP.

              DELETE FROM ymoont_api020.
              MODIFY ymoont_api020 FROM TABLE @lt_dbtab.


            ENDLOOP.

        ENDCASE.
      ENDLOOP.
    ELSE.
      IF lv_search IS INITIAL.
        lv_search = '21EASK14G1I60490001'.
      ENDIF.
      call_out_api( IMPORTING out_tab = lt_out search_str = lv_search ).
      LOOP AT lt_out INTO DATA(ls_out2).
        CLEAR ls_dbtab.
        MOVE-CORRESPONDING ls_out2 TO ls_dbtab.
        TRY.
            ls_dbtab-uuid = lo_system_uuid->if_system_uuid~create_uuid_x16( ).
          CATCH cx_uuid_error.
        ENDTRY.

        ls_dbtab-regid = sy-uname.
        ls_dbtab-regdate = sy-datum.
        ls_dbtab-cargmtno = lv_search.
        APPEND ls_dbtab TO lt_dbtab.


      ENDLOOP.

      DELETE FROM ymoont_api020.
      MODIFY ymoont_api020 FROM TABLE @lt_dbtab.

    ENDIF.




    get_data_from_request( EXPORTING io_request = io_request IMPORTING rt_result = lt_database ).
*
    CLEAR lt_output.
    MOVE-CORRESPONDING lt_database TO lt_output.
*    LOOP AT lt_database INTO DATA(ls_line).
*      CLEAR ls_output.
*      MOVE-CORRESPONDING ls_line TO ls_output.
*      APPEND ls_output TO lt_output.
*    ENDLOOP.



    IF io_request->is_data_requested( ).
      io_response->set_data( lt_output ).
    ENDIF.

    IF io_request->is_total_numb_of_rec_requested( ).
      io_response->set_total_number_of_records( lines( lt_output ) ).
    ENDIF.
  ENDMETHOD.
ENDCLASS.
